package com.qiwenshare.file.controller;



import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.SpiderService;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.domain.Department;
import com.qiwenshare.file.domain.Spider;
import com.qiwenshare.file.dto.spider.AddSpiderDto;
import com.qiwenshare.file.dto.spider.DeleteSpiderDto;
import com.qiwenshare.file.dto.spider.ModifySpiderDto;
import com.qiwenshare.file.util.SpringBeanUtil;
import com.qiwenshare.file.vo.SpiderVo;
import com.qiwenshare.file.vo.user.UserLoginVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 爬虫表(Spider)表控制层
 *
 * @author makejava
 * @since 2023-09-24 19:35:20
 */
@RestController
@RequestMapping("spider")
public class SpiderController{
    /**
     * 服务对象
     */
    @Resource
    private SpiderService spiderService;

    public static final String CURRENT_MODULE = "爬虫管理";

    @Operation(summary = "分页查询爬虫", description = "分页查询爬虫", tags = {"spider"})
    @GetMapping(value = "/page")
    @MyLog(operation = "分页查询爬虫", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<SpiderVo> queryPage(
            @Parameter(description = "当前页", required = true) long currentPage,
            @Parameter(description = "页面数量", required = true) long pageCount) {
        IPage<SpiderVo> page = spiderService.queryPage(currentPage, pageCount);
        List<SpiderVo> list = page.getRecords().stream().peek(u -> {
            u.setCreateUser(InitData.getStaffNameByUserId(u.getCreateUserId()));
        }).collect(Collectors.toList());
        return RestResult.success().dataList(list, page.getTotal());
    }

    @Operation(summary = "新增爬虫", description = "新增爬虫", tags = {"spider"})
    @PostMapping(value = "/add")
    @MyLog(operation = "新增爬虫", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> add(@RequestBody AddSpiderDto spiderDto) {
        Spider spider = SpringBeanUtil.copyProperties(spiderDto, Spider.class);
        spider.setSpiderId(IdUtil.getSnowflakeNextIdStr());
        spider.setCreateUserId(SessionUtil.getUserId());
        spider.setCreateTime(DateUtil.getCurrentTime());
        return spiderService.save(spider) ? RestResult.success() : RestResult.fail();
    }


    @Operation(summary = "编辑爬虫", description = "编辑爬虫", tags = {"spider"})
    @PostMapping(value = "/modify")
    @MyLog(operation = "编辑爬虫", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> modify(@RequestBody ModifySpiderDto spiderDto) {
        Spider spider = SpringBeanUtil.copyProperties(spiderDto, Spider.class);
        spider.setModifyUserId(SessionUtil.getUserId());
        spider.setModifyTime(DateUtil.getCurrentTime());
        return spiderService.update(new QueryWrapper<>(spider)) ? RestResult.success() : RestResult.fail();
    }

    @Operation(summary = "删除爬虫", description = "删除爬虫", tags = {"spider"})
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除爬虫", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> delete(@RequestBody DeleteSpiderDto spiderDto) {
        return spiderService.remove(new QueryWrapper<>(Spider.builder()
                .spiderId(spiderDto.getSpiderId())
                .build())) ? RestResult.success() : RestResult.fail();
    }
}

